package com.xyy.yueshijia.topic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.google.gson.Gson;
import com.xyy.yueshijia.R;
import com.xyy.yueshijia.myutil.HttpResponse;
import com.xyy.yueshijia.myutil.MyHttpUtils;
import com.xyy.yueshijia.myutil.NetConfig;
import com.xyy.yueshijia.topic.topicbean.MapAdapter;
import com.xyy.yueshijia.topic.topicbean.MapBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/2/14.
 */

public class MapFragment extends Fragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshView)
    SwipeRefreshLayout swipeRefreshView;
    private ArrayList<MapBean> datas;
    private MapAdapter adapter;

    public MapFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.houdong_fragment, container, false);
        ButterKnife.bind(this, view);
        datas = new ArrayList<>();
        return view;
    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        swipeRefreshView.setColorSchemeColors(getResources().getColor(R.color.red));
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MapAdapter(getContext(), datas);
        recyclerView.setAdapter(adapter);
        swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData();
            }
        });

        swipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData();
                swipeRefreshView.setRefreshing(false);
            }
        });

        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //在布局加载完成之后，才能执行，不然看不到效果
                swipeRefreshView.setRefreshing(true);
                loadData();
                swipeRefreshView.setRefreshing(false);
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    private void loadData() {

        HashMap<String, String> params = new HashMap<>();
        params.put("act", "app");
        params.put("op", "special_programa");
        params.put("v", "2.1.2");
        params.put("req_time", "1486533706420");
        params.put("special_type", "5");
        params.put("key", "");
        params.put("channel", "yingyongbao");
        params.put("token", "749a036dc06ae8b3a120848995a9f306");
        params.put("client", "android");

        MyHttpUtils.getInstance().get(NetConfig.BASE_URL, "", params, new HttpResponse() {
            @Override
            public void onResponse(String data) {
                Log.e("====地图json", "====地图json" + data);

                try {
                    JSONObject jsonObject =new JSONObject(data);

                    JSONObject jsonObject1 = jsonObject.optJSONObject("datas");
                    JSONArray jsonArray = jsonObject1.optJSONArray("article_list");

                    Gson gson =new Gson();
                    for(int i=0;i<jsonArray.length();i++){
                        String s = jsonArray.get(i).toString();
                        datas.add(gson.fromJson(s,MapBean.class));
                    }
adapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(String msg) {

            }
        });


    }


}
